package com.atstudent.gmall.product.service.impl;

import com.atstudent.gmall.product.dto.SpuInfoDto;
import com.atstudent.gmall.product.entity.SpuImage;
import com.atstudent.gmall.product.entity.SpuSaleAttr;
import com.atstudent.gmall.product.entity.SpuSaleAttrValue;
import com.atstudent.gmall.product.service.SpuImageService;
import com.atstudent.gmall.product.service.SpuSaleAttrService;
import com.atstudent.gmall.product.service.SpuSaleAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atstudent.gmall.product.entity.SpuInfo;
import com.atstudent.gmall.product.service.SpuInfoService;
import com.atstudent.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 薛有朋
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2024-11-17 20:40:42
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements SpuInfoService{

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public Page findByPage(Integer pageNum, Integer pageSize, Long category3Id) {
        //分页带条件出查询
        LambdaQueryWrapper<SpuInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpuInfo::getCategory3Id , category3Id);
        Page page = new Page<>();
        this.page(page , lambdaQueryWrapper);
        return page;
    }

    @Override
    @Transactional
    public void saveSpuInfo(SpuInfoDto spuInfoDto) {
        //1.保存spuinfo的数据
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoDto , spuInfo);
        this.save(spuInfo);

        //2.保存图片数据
        List<SpuImage> spuImageList = spuInfoDto.getSpuImageList();
        spuImageList = spuImageList.stream().map(spuImage -> {
            //补全缺少的数据
            spuImage.setSpuId(spuInfo.getId());
            return spuImage;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //3.保存销售属性的数据
        List<SpuSaleAttr> spuSaleAttrList = spuInfoDto.getSpuSaleAttrList();
        spuSaleAttrList = spuSaleAttrList.stream().map(spuSaleAttr -> {
            spuSaleAttr.setSpuId(spuInfo.getId());
            return spuSaleAttr;
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);

        //4.保存销售属性值的数据 遍历销售属性 拿到每一个销售属性 然后get到每个销售属性值的list  再对此销售属性值的list进行补全数据
        spuSaleAttrList.forEach(spuSaleAttr -> {
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            spuSaleAttrValueList = spuSaleAttrValueList.stream().map(spuSaleAttrValue -> {
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                return spuSaleAttrValue;
            }).collect(Collectors.toList());
            spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
        });
    }
}




